package saxion.speelveld.spaceengineer.database;

import java.util.ArrayList;

import saxion.speelveld.spaceengineer.controller.AsyncTaskManager;
import saxion.speelveld.spaceengineer.model.TradeDeal;
import saxion.speelveld.spaceengineer.model.TradePlanet;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TradePlanetsDAO
{
	private SQLiteDatabase database;

	private TradePlanetsDB dbHelper;

	private TradeDealsDAO tradeDealsDatasource;

	private static int connectionsOpen = 0;

	public TradePlanetsDAO(Context context)
	{
		dbHelper = new TradePlanetsDB(context);
		tradeDealsDatasource = new TradeDealsDAO(context);
	}

	/**
	 * Open the database connection.
	 * 
	 * @throws SQLException
	 */
	public void open() throws SQLException
	{
		database = dbHelper.getWritableDatabase();
		connectionsOpen += 1;
		Log.e("TradePlanetsDAO.open", "open connections: " + connectionsOpen);
	}

	/**
	 * Close the database connection.
	 */
	public void close()
	{
		dbHelper.close();
		connectionsOpen -= 1;
		Log.e("TradePlanetsDAO.open", "open connections: " + connectionsOpen);
	}

	public void putTradePlanetsForGame(int gameId, TradePlanet[] tradePlanets)
	{
		try
		{

			tradeDealsDatasource.open();
			tradeDealsDatasource.beginTransactionMode();
			for (TradePlanet tp : tradePlanets)
			{
				ContentValues values = new ContentValues();
				values.put(TradePlanetsDB.COLUMN_GAMEID, gameId);
				values.put(TradePlanetsDB.COLUMN_PLANETNAME, tp.getPlanetName());
				values.put(TradePlanetsDB.COLUMN_PLANETIMAGE, tp.getPlanetImage());
				long id = database.insert(TradePlanetsDB.TABLE_NAME, null, values);
				Log.i("putTradePlanetsForGame", "inserted planet " + id + " - "
					+ AsyncTaskManager.getAsyncTaskStatus().toString());

				for (TradeDeal td : tp.getDeals())
				{
					tradeDealsDatasource.addTradeDealForPlanet((int) id, td.tradeType, td.cost);
				}

			}
			// Log.e("asfasf", "asfasfasf");
			tradeDealsDatasource.setTransactionModeSuccesfull();
		}
		finally
		{
			tradeDealsDatasource.endTransactionMode();
			tradeDealsDatasource.close();
		}

	}

	public ArrayList<TradePlanet> getAllTradePlanetsForGame(int gameId)
	{
		ArrayList<TradePlanet> listOfTradePlanets = new ArrayList<TradePlanet>();

		Cursor cursor = database.rawQuery("SELECT * FROM " + TradePlanetsDB.TABLE_NAME + " WHERE "
			+ TradePlanetsDB.COLUMN_GAMEID + " = " + gameId, null);

		// Cursor cursor = database.rawQuery("SELECT * FROM " + TradePlanetsDB.TABLE_NAME, null);

		cursor.moveToFirst();

		tradeDealsDatasource.open();
		while (cursor.isAfterLast() == false)
		{
			listOfTradePlanets.add(cursorToTradePlanet(cursor));
			cursor.moveToNext();
		}
		tradeDealsDatasource.close();
		cursor.close();
		return listOfTradePlanets;
	}

	private TradePlanet cursorToTradePlanet(Cursor cursor)
	{
		TradePlanet tradePlanet = null;

		int planetId = cursor.getInt(cursor.getColumnIndex(TradePlanetsDB.COLUMN_ID));
		String name = cursor.getString(cursor.getColumnIndex(TradePlanetsDB.COLUMN_PLANETNAME));
		int imageId = cursor.getInt(cursor.getColumnIndex(TradePlanetsDB.COLUMN_PLANETIMAGE));
		tradePlanet = new TradePlanet(name);
		tradePlanet.setPlanetImage(imageId);

		ArrayList<TradeDeal> tradeDeals = tradeDealsDatasource.getAllTradeDealsForPlanet(planetId);

		for (TradeDeal tradeDeal : tradeDeals)
		{
			tradePlanet.AddtradeDeal(tradeDeal.tradeType, 200, tradeDeal.cost);
		}

		return tradePlanet;
	}
}
